
var WindPanel = new Object();

WindPanelModel = function() {
	this.entries = new Array();
	this.title = "";
}

WindPanelModel.prototype.setTitle = function(title) {
	this.title = title;
} 

WindPanelModel.prototype.getTitle = function() {
	return this.title;
} 

WindPanelModel.prototype.getEntries = function() {
	return this.entries;
} 

WindPanelModel.prototype.addSeparator = function() {
	var entry = new WindPanelEntryModel();
	entry.type = "separator";
	this.entries[this.entries.length] = entry ;
} 

WindPanelModel.prototype.addTextEntry = function(text) {
	var entry = new WindPanelEntryModel();
	entry.type = "text";
	entry.setText(text);
	this.entries[this.entries.length] = entry;
	return entry;
}

WindPanelModel.prototype.addActionEntry = function(text, action) {
	var entry = new WindPanelEntryModel();
	entry.type = "action";
	entry.setText(text);
	entry.setAction(action);
	this.entries[this.entries.length] = entry;
	return entry;
}

WindPanelModel.prototype.addUrlEntry = function(text, url) {
	var entry = new WindPanelEntryModel();
	entry.type = "url";
	entry.setText(text);
	entry.setURL(url);
	this.entries[this.entries.length] = entry;
	return entry;
}

WindPanelEntryModel = function() {
	this.type = "text";
	this.entries = new Array();
}

WindPanelEntryModel.prototype.getType = function() {
	return this.type;
}

WindPanelEntryModel.prototype.setType = function(type) {
	this.type = type;
}

WindPanelEntryModel.prototype.getText = function() {
	return this.text;
}

WindPanelEntryModel.prototype.setText = function(text) {
	this.text = text;
}

WindPanelEntryModel.prototype.getAction = function() {
	return this.action;
}

WindPanelEntryModel.prototype.setAction = function(action) {
	this.action = action;
}

WindPanelEntryModel.prototype.getURL = function() {
	return this.url;
}

WindPanelEntryModel.prototype.setURL = function(url) {
	this.url = url;
}

WindPanelEntryModel.prototype.getEntries = function() {
	return this.entries;
} 

WindPanelEntryModel.prototype.addTextEntry = function(text) {
	var entry = new WindPanelEntryModel();
	entry.type = "text";
	entry.setText(text);
	this.entries[this.entries.length] = entry;
	return entry;
}

WindPanelEntryModel.prototype.addLinkEntry = function(text, action) {
	var entry = new WindPanelEntryModel();
	entry.type = "link";
	entry.setText(text);
	entry.setAction(action);
	this.entries[this.entries.length] = entry;
	return entry;
}

WindPanel.createModel = function() {
	return new WindPanelModel;
}

WindPanel.createView = function(panelModel) {
	var panelView = new WindView();
	panelView.setCssClass("WindPanel");
	
	var title = panelModel.getTitle();
	if(title!=null && title.length>0) {
		var titleView = new WindView();
		titleView.setCssClass("WindPanelTitle");
		titleView.setText(title);
		panelView.addView(titleView);
	}
	
	for(var i=0; i<panelModel.getEntries().length; i++) {
		var entryModel = panelModel.getEntries()[i];
		var entryView = new WindView();

		if(entryModel.getEntries().length>0) {
			var expandView = new WindView();
			expandView.setCssClass("WindPanelEntryExpandButton");
			expandView.setImage("wind/img/WindExpand2.png");
			entryView.addView(expandView);
		}

		if(entryModel.type == "separator") {
			entryView.setCssClass("WindPanelEntrySeparator");
		} else if (entryModel.type == "text") {
			entryView.setCssClass("WindPanelEntry");
			entryView.setText(entryModel.getText());
		} else if (entryModel.type == "action") {
			entryView.setCssClass("WindPanelEntry");
			var linkView = WindLink.createViewFromIdText(null, entryModel.getText());
			linkView.onClick = entryModel.getAction();
			entryView.addView(linkView);
		} else if (entryModel.type == "url") {
			entryView.setCssClass("WindPanelEntry");
			var linkView = WindLink.createViewFromIdTextURL(null, entryModel.getText(), entryModel.getURL());
			entryView.addView(linkView);
		}
		
		if(entryModel.getEntries().length>0) {
			for(var j=0; j<entryModel.getEntries().length; j++) {
				var childEntryModel = entryModel.getEntries()[j];
				var childEntryView = new WindView();
				if(childEntryModel.type == "text") {
					childEntryView.setCssClass("WindPanelEntry");
					childEntryView.setText(childEntryModel.getText());
				} else if (childEntryModel.type == "action") {
					childEntryView.setCssClass("WindPanelEntry");
					var linkView = WindLink.createViewFromIdText(null, childEntryModel.getText());
					linkView.onClick = entryModel.getAction();
					childEntryView.addView(linkView);					
				} else if (childEntryModel.type == "url") {
					childEntryView.setCssClass("WindPanelEntry");
					var linkView = WindLink.createViewFromIdTextURL(null, childEntryModel.getText(), childEntryModel.getURL());
					childEntryView.addView(linkView);					
				}
				entryView.addView(childEntryView);
			}
		}
		panelView.addView(entryView);
	}
	
	return panelView;
}
